/**
 Copyright (c) 2010, C.A. Burrell
 All rights reserved.

 Redistribution and use in source and binary forms, with or without modification,
 are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following
 * disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
 * following disclaimer in the
 documentation and/or other materials provided with the distribution.
 * Neither the name of "c-a-fe.ca" nor the names of its contributors may be used to endorse or promote products
 * derived from this
 software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO,
 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 COPYRIGHT HOLDER OR CONTRIBUTORS
 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
 */
/**
 * @author C.A. Burrell (deltafront@gmail.com)
 */

package gaboli.common.utils;

import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import static gaboli.common.utils.StringUtils.isEmpty;

/**
 * Contains utilities for evaluation of string content.
 *
 * @author C. Burrell (deltafront@gmail.com)
 */
public class SimpleRegexUtils
{
    /**
     * Tests to see if this string consists entirely of alphanumeric characters.
     *
     * @param string String to evaluate.
     * @return true if this string consists entirely of alphanumeric characters.
     * @since 1.0
     */
    public static boolean isAlphaNSeries( String string )
    {
        if( isEmpty( string ) )
        {
            return false;
        }
        for( int i = 0; i < string.length(); i++ )
        {
            char _char = string.toLowerCase().charAt( i );
            if( !isNumber( _char ) && !isLetter( _char ) )
            {
                return false;
            }
        }
        return true;
    }

    /**
     * Tests to see if the string consists entirely of letters.
     *
     * @param string String to evaluate.
     * @return true if the string consists entirely of letters.
     * @since 1.0
     */
    public static boolean isLetterSeries( String string )
    {
        if( isEmpty( string ) )
        {
            return false;
        }
        for( int i = 0; i < string.length(); i++ )
        {
            if( !isLetter( string.charAt( i ) ) )
            {
                return false;
            }
        }
        return true;
    }

    /**
     * Determines whether this string is comprised of all numerical digits
     *
     * @param string String to evaluate.
     * @return true if this string represents a valid number series.
     * @since 1.0
     */
    public static boolean isValidNumberSeries( String string )
    {
        if( isEmpty( string ) )
        {
            return false;
        }
        boolean isValid = true;
        boolean isESeries = string.toLowerCase().contains( "e" );
        char[] chars = string.toCharArray();
        boolean eflag = false;
        int negcount = 0;
        boolean decimalflag = false;
        for( char c : chars )
        {
            if( c == '-' && ( isESeries ) ? negcount < 2 : negcount < 1 )
            {
                negcount++;
                continue;
            }
            else if( ( c == 'E' || c == 'e' ) && !eflag )
            {
                eflag = true;
                continue;
            }
            else if( c == '.' && !decimalflag )
            {
                decimalflag = true;
                continue;
            }
            else if( !Character.isDigit( c ) )
            {
                isValid = false;
                break;
            }
        }
        return isValid;
    }

    /**
     * Determines if the input string conforms to the format string. The format string must be a valid regular
     * expression,
     *
     * @param format format string.
     * @param input  string that is supposed to adhere to format string.
     * @return true if input is of 'format' format.
     * @since 1.0
     */
    public static boolean isValidFormattedSeries( String format, String input )
    {
        if( isEmpty( format ) || isEmpty( input ) )
        {
            return false;
        }
        boolean isValid = true;
        try
        {
            isValid = Pattern.matches( format, input );
        }
        catch( PatternSyntaxException e )
        {
            isValid = false;
        }
        return isValid;
    }


    private static boolean isLetter( char _char )
    {
        return Character.isLetter( _char );
    }

    private static boolean isNumber( char _char )
    {
        return Character.isDigit( _char );
    }
}